#include "selector.h"

ControllerSelector::ControllerSelector(Interface * interface,Commando * cmd)
                : Controller(interface,cmd)
{
    MIN_OPT = 0;
    MAX_OPT = 2;
}

int ControllerSelector::execute(){
    int opcao = 0;
    stringstream ss;
    exception exc;
    bool tf = true;
    Builder * builder;
    Controller * cntr;

    while(tf){
        try{
            opcao = 0;
            opcao = interface->chooseOpt();
            if(opcao>MAX_OPT || opcao<MIN_OPT){
                ss << endl << "opcao invalida :" << opcao << endl;
                interface->inform(ss.str());
                break;
            }
        }catch (exception& e){

            ss << endl << "Erro (ControllerSelector.execute.try.chooseOpt):" << endl;
            ss << e.what() << endl;

            interface->inform(ss.str());
            break;
        }

        switch(opcao){
            case 1:
                builder = new BuilderPhoto();
                break;
            case 2:
                builder = new BuilderAlbum();
                break;
            default:
                exit(1);
        }

        cntr = builder->criar();

        try{
            cntr->execute();
        }catch(string e){
            ss.str("");
            ss << e << endl;
            interface->inform(ss.str());
        }catch(char* e){
            ss.str("");
            ss << e << endl;
            interface->inform(ss.str());
        }
    }
    return 0;
}

builderSelector::builderSelector(){
}

Controller * builderSelector::criar() const{
    Interface * interface = new Interface("Menu Inicial","\t0-sair\n\t1-Photo\n\t2-Album");
    Controller * cntr = new ControllerSelector(interface,0);

    return cntr;
}
